package top.callname.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import top.callname.bean.ConstanstBean;
import top.callname.bean.CourseBean;
import top.callname.bean.CourseInfoBean;
import top.callname.bean.CourseTimeBean;
import top.callname.bean.StudentBean;
import top.callname.dao.CourseDao;
import top.callname.dao.StudentDao;
import top.callname.service.CourseService;
import top.callname.util.PropertiesUtil;

/**
 * 查询课表
 *
 */
public class CourseServiceImpl implements CourseService{
	
	private CourseDao courseDao = new CourseDao();
	private StudentDao studentDao = new StudentDao();
	private Logger logger = Logger.getLogger(CourseServiceImpl.class);

	private String[] courseKeys = {
										"courseOne",
										"courseTwo",
										"courseThree",
										"courseFour",
										"courseFive",
										"courseSix",
										"courseSeven",
										"courseEight"};
	
	@Override
	public CourseInfoBean getCourseTableByCid(Integer cid) {
		CourseInfoBean info = new CourseInfoBean();
		List<CourseBean> list = courseDao.selectByCid(cid);
		Map<String, CourseBean> courseInfo = new HashMap<String, CourseBean>();
		Map<String, CourseTimeBean> timeInfo = new HashMap<String, CourseTimeBean>();
		for (CourseBean obj : list) {
			courseInfo.put("day_"+obj.getWeekDay(), obj);
		}
		for (String key : courseKeys) {
			timeInfo.put(key, courseDao.selectCourseTimeByKey(key));
		}
		info.setTimeMap(timeInfo);
		info.setCourseMap(courseInfo);
		logger.debug("查询课程表："+info.toString());
		return info;
	}

	@Override
	public CourseInfoBean getNowDayCourseTableByOpenid(String openid) {
		//查询该用户是否存在系统中
		CourseInfoBean courseInfoBean = new CourseInfoBean();
		StudentBean bean = studentDao.selectByOpenid(openid);
		
		if(bean != null){
			CourseService courseService = new CourseServiceImpl();
			courseInfoBean = courseService.getCourseTableByCid(bean.getCid());
			courseInfoBean.setRspCode(ConstanstBean.RSP_SUCCESS);
			//获取当前是一周的第几天
			int weekDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
			if(weekDay==0){
				//周日
				String content = "今天周日，没有课哦，放松一下吧！\n";
				content += "点击查看课程表："+PropertiesUtil.getHostUrl()+"/common?method=course&openid="+openid;
				courseInfoBean.setRspDesc(content);
				
			}else if(weekDay==6){
				//周六
				String content = "今天周六，没有课哦，放松一下吧！\n";
				content += "点击查看课程表："+PropertiesUtil.getHostUrl()+"/common?method=course&openid="+openid;
				courseInfoBean.setRspDesc(content);
				
			}else{
				CourseBean course = courseInfoBean.getCourseMap().get("day_"+weekDay);
				Map<String, CourseTimeBean> map = courseInfoBean.getTimeMap();
				String content = "今日课程：\n";
				content += "---------上午--------\n";
				content += course.getCourseOne()+"("+map.get("courseOne").getStartTime()+"--"
				+map.get("courseOne").getEndTime()+") \n";
				content += course.getCourseTwo()+"("+map.get("courseTwo").getStartTime()+"--"
						+map.get("courseTwo").getEndTime()+") \n";
				content += course.getCourseThree()+"("+map.get("courseThree").getStartTime()+"--"
						+map.get("courseThree").getEndTime()+") \n";
				content += course.getCourseFour()+"("+map.get("courseFour").getStartTime()+"--"
						+map.get("courseFour").getEndTime()+") \n";
				
				content += "---------下午--------\n";
				
				content += course.getCourseFive()+"("+map.get("courseFive").getStartTime()+"--"
						+map.get("courseFive").getEndTime()+") \n";
						content += course.getCourseSix()+"("+map.get("courseSix").getStartTime()+"--"
								+map.get("courseSix").getEndTime()+") \n";
						content += course.getCourseSeven()+"("+map.get("courseSeven").getStartTime()+"--"
								+map.get("courseSeven").getEndTime()+") \n";
						content += course.getCourseEight()+"("+map.get("courseEight").getStartTime()+"--"
								+map.get("courseEight").getEndTime()+") \n";
						content += "点击查看课程表："+PropertiesUtil.getHostUrl()+"/common?method=course&openid="+openid;
				courseInfoBean.setRspDesc(content);
			
			}
			
		} else{
			courseInfoBean.setRspDesc("该用户不存在系统中，请先注册！");
		}
		return courseInfoBean;
	}

}
